require(['/js/config.js'],()=>{
    require(['header','protect','footer'],()=>{
        class Login{
            constructor(){
                this.btn=$('#loginBtn')
                this.login()
            }
            login(){
                this.btn.on('click',()=>{
                    this.username=$('#loginUser').val()
                    this.password=$('#loginPwd').val()
                    this.remember=$('#remember').prop('checked')
                    const user = JSON.parse(localStorage.getItem('user'))
                    
                    if(user){
                        const isExist=user.some(item=>{
                            return item.username==this.username
                        })
                        const verify=user.some(item=>{
                            return item.username==this.username&&item.password==this.password
                        })
                        if(isExist){
                            if(verify){
                                if(this.remember){
                                    const d = new Date()
                                    d.setDate(d.getDate()+7)
                                    document.cookie=`username=${this.username};expires=${d.toUTCString()};path=/`
                                }
                                else{
                                    document.cookie=`username=${this.username};path=/`
                                }
                                this.success()
                            }
                            else{
                                this.fail()
                            }
                        }else{
                            this.notRegister()
                        }
                    }else{
                        this.notRegister()
                    }
                    
                })
            }
            notRegister(){
                $('#loginInfo').html('未注册 即将跳转注册页面').addClass('fail')

                setInterval(()=>{
                    $('#loginInfo').html('&nbsp;').removeClass('fail')
                    location.replace('/html/register.html')
                },3000)
            }
            success(){
                $('#loginInfo').html('登录成功，即将返回首页').addClass('sucess')

                setInterval(()=>{
                    $('#loginInfo').html('&nbsp;').removeClass('sucess')
                    location.replace('/index.html')
                },3000)
            }
            fail(){
                $('#loginInfo').html('账号或密码错误,请重试').addClass('fail')

                setInterval(()=>{
                    $('#loginInfo').html('&nbsp;').removeClass('fail')
                },3000)
            }
        }
        return new Login()
    })
})